<?php

namespace app\data\service;

use think\admin\Service;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;

/**
 * 短信支持服务
 * Class MessageService
 * @package app\data\service
 */
class ALiMsgService extends Service
{
    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    /**
     * 发送验证码
     * @param $mobile
     * @param $code
     * @return bool
     */
    public static function sendSms($mobile, $code){
        $client = self::createClient("LTAI5tJZApB1MXgTrq47pxgv", "URcbBywgKOB6saSwmxuVAWUv80JWI8");
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => $mobile,
            "signName" => "豪达寄存柜",
            "templateCode" => "SMS_215341401",
            "templateParam" => "{\"code\":\"{$code}\"}"
        ]);
        $resp = $client->sendSms($sendSmsRequest);
        if(strtolower($resp->body->message??'')=='ok' && strtolower($resp->body->code??'')=='ok'){
            return true;
        }else{
            return false;
        }
    }
}